﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.ComponentModel;

namespace Ling.P2P.DemoLib
{
    /// <summary>
    /// 表示客户端信息集合。
    /// </summary>
    public class ClientInfoCollections : BindingList<ClientInfo>
    {
        /// <summary>
        /// 判断指定的客户端在集合中是否存在。
        /// </summary>
        /// <param name="client">客户端。</param>
        /// <returns></returns>
        public bool Exists(ClientInfo client)
        {
            foreach (ClientInfo c in this)
            {
                if (c.Name == client.Name)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 从缓合中查找指定名称的客户端。
        /// </summary>
        /// <param name="clientName">客户端名称。</param>
        /// <returns></returns>
        public ClientInfo FindClientByName(string clientName)
        {
            foreach (ClientInfo c in this)
            {
                if (c.Name == clientName)
                    return c;
            }
            return null;
        }

        /// <summary>
        /// 将客户端添加到集合中。
        /// </summary>
        /// <param name="item"></param>
        public new void Add(ClientInfo item)
        {
            if (!Exists(item))
                base.Add(item);
        }

        public new void Insert(int index, ClientInfo item)
        {
            if (!Exists(item))
                base.Insert(index, item);
        }

        /// <summary>
        /// 将集合序列化为字节数组。
        /// </summary>
        /// <param name="clients">要序列化的集合。</param>
        /// <returns></returns>
        public static byte[] Serialization(ClientInfoCollections clients)
        {
            if (clients == null) return null;

            StringBuilder content = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            content.Append("<ClientInfoCollections>");
            foreach (ClientInfo c in clients)
                content.AppendFormat("<ClientInfo Name=\"{0}\" IPAddress=\"{1}\" Port=\"{2}\" />", c.Name, c.IPAddress, c.Port);
            content.Append("</ClientInfoCollections>");

            byte[] data = Encoding.UTF8.GetBytes(content.ToString());
            return data;
        }

        /// <summary>
        /// 将指定的字节数组反序列化为客户端集合。
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ClientInfoCollections Deserialization(byte[] data)
        {
            if (data == null) return null;

            string content = Encoding.UTF8.GetString(data);
            XmlDocument XDoc = new XmlDocument();
            XDoc.LoadXml(content);
            ClientInfoCollections clients = new ClientInfoCollections();
            XmlNodeList items = XDoc.SelectNodes("/ClientInfoCollections/ClientInfo");
            foreach (XmlNode n in items)
            {
                ClientInfo c = new ClientInfo();
                c.Name = n.Attributes["Name"].Value;
                c.IPAddress = n.Attributes["IPAddress"].Value;
                c.Port = string.IsNullOrEmpty(n.Attributes["Port"].Value) ? 0 : int.Parse(n.Attributes["Port"].Value);
                clients.Add(c);
            }
            return clients;
        }
    }
}
